package leetcode.code0939;

import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	int Len = (int) 1e4 * 4 + 1;

	public int minAreaRect(int[][] points) {
		// [[1,1],[1,3],[3,1],[3,3],[2,2]]
//		Arrays.sort(points, (a, b) -> a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]);
		int len = points.length;
		int ans = Integer.MAX_VALUE;
		Set<Integer> hash = new HashSet<>();
		for (int i = 0; i < len; i++) {
			int[] p1 = points[i];
			int x1 = p1[0], y1 = p1[1];
			hash.add(x1 * Len + y1);
		}
		for (int i = 0; i < len; i++) {
			int[] p1 = points[i];
			int x1 = p1[0], y1 = p1[1];
			for (int j = i + 1; j < len; j++) {
				int[] p2 = points[j];
				int x2 = p2[0], y2 = p2[1];
				if (x1 != x2 && y1 != y2 && hash.contains(x1 * Len + y2) && hash.contains(x2 * Len + y1)) {
					ans = Math.min(ans, Math.abs(x2 - x1) * Math.abs(y2 - y1));
				}
			}
		}
//		[[3,2],[3,1],[4,4],[1,1],[4,3],[0,3],[0,2],[4,0]]
		return ans == Integer.MAX_VALUE ? 0 : ans;// 面壁思过
	}

	@Override
	public void debug1() {

		HelpDebug.compare(4, this.minAreaRect(HelpDebug.str2array1("[[1,1],[1,3],[3,1],[3,3],[2,2]]")));
	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub
		HelpDebug.compare(2, this.minAreaRect(HelpDebug.str2array1("[[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub
		HelpDebug.compare(96894, this.minAreaRect(HelpDebug.str2array1(
				"[[16574,14889],[16647,25422],[8979,14889],[22226,16547],[22226,14889],[16647,24259],[19768,3426],[19768,25422],[8979,10409],[22226,24259],[17599,16547],[8979,8631],[22226,10409],[17599,1973],[16647,8631],[29472,8631],[29472,14889],[38102,16547],[16647,14889],[16574,1973],[16574,25422],[27869,14889],[27869,17193],[38102,10409],[19768,16547],[27869,3426],[19768,8631],[27869,24259],[29472,17193],[17599,24259],[16647,3426],[16647,10409],[38102,24259],[19768,3177],[38102,3426],[22226,23468],[17599,10409],[22240,16547],[22240,23468],[22240,3177],[27869,23468],[8979,16547],[22226,17193],[38102,8631],[29472,16547],[8979,17193],[19768,14889],[16647,16547],[29472,10409],[29472,1973],[16647,23468],[27869,3177],[22226,3426],[22226,25422],[16574,8631],[16574,3426],[17599,23468],[38102,14889],[8979,24259],[17599,3426],[29472,25422],[27869,25422],[22226,1973],[16647,17193],[29472,3426],[16647,3177]]")));

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
